package com.xh.jf_blog.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xh.jf_blog.bean.Code;
import com.xh.jf_blog.bean.Userinfo;
import com.xh.jf_blog.common.exception.RTException;
import com.xh.jf_blog.common.form.LoginForm;
import com.xh.jf_blog.common.form.RegisterForm;
import com.xh.jf_blog.common.util.R;
import com.xh.jf_blog.mapper.ArticleLikeMapper;
import com.xh.jf_blog.mapper.CommentMapper;
import com.xh.jf_blog.mapper.UserinfoMapper;
import com.xh.jf_blog.service.CodeService;
import com.xh.jf_blog.service.UserinfoService;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @Author PangJunjie
 * @Date 2024/1/31/031
 */
@Service
@Transactional
public class UserinfoServiceImpl implements UserinfoService {
    @Autowired
    private UserinfoMapper userinfoMapper;
    @Autowired
    private CodeService codeService;
    @Autowired
    private ArticleLikeMapper articleLikeMapper;
    @Autowired
    private CommentMapper commentMapper;


    public Userinfo login(LoginForm loginForm) {
        Code code = new Code();
        BeanUtils.copyProperties(loginForm, code);
        if (codeService.checkCode(code)) {
            Userinfo userinfo = new Userinfo();
            BeanUtils.copyProperties(loginForm, userinfo);
            userinfo.setPassword(DigestUtils.md5Hex(userinfo.getPassword()));
            return userinfoMapper.login(userinfo);
        }
        return null;
    }

    public R getPage(int page, int limit, Userinfo userinfo) {
        PageHelper.startPage(page, limit);
        List<Userinfo> list = userinfoMapper.getAll(userinfo);
        PageInfo<Userinfo> pageInfo = new PageInfo<Userinfo>(list);
        return R.page(pageInfo.getTotal(), pageInfo.getList());
    }

    public Userinfo getById(int userinfoId) {
        return userinfoMapper.selectByPrimaryKey(userinfoId);
    }

    public void saveOrUpdate(Userinfo userinfo) {
        if (userinfo.getUserinfoId() == null) {
            /*添加时先验证是否重名*/
            Userinfo user = new Userinfo();
            user.setUsername(userinfo.getUsername());
            if (userinfoMapper.getAll(user).isEmpty()) {
                userinfo.setPassword(DigestUtils.md5Hex("123456"));
                userinfo.setRole(1);
                userinfoMapper.insertSelective(userinfo);
            } else {
                throw new RTException("用户名已存在！");
            }
        } else {
            if (StringUtils.isNotEmpty(userinfo.getPassword())) {
                userinfo.setPassword(DigestUtils.md5Hex(userinfo.getPassword()));
            }
            userinfoMapper.updateByPrimaryKeySelective(userinfo);
        }
    }

    public void deleteById(int userinfoId) {
        userinfoMapper.deleteByPrimaryKey(userinfoId);
        commentMapper.deleteByUserinfoId(userinfoId);
        articleLikeMapper.deleteByOptId(userinfoId);
        articleLikeMapper.deleteFollowByUserinfoId(userinfoId);
    }

    public R checkPassword(Userinfo userinfo) {
        userinfo.setPassword(DigestUtils.md5Hex(userinfo.getPassword()));
        List<Userinfo> list = userinfoMapper.getAll(userinfo);
        return list.isEmpty() ? R.error("原密码不正确") : R.ok();
    }

    public void register(RegisterForm registerForm) {
        Code code = new Code();
        BeanUtils.copyProperties(registerForm, code);
        if (codeService.checkCode(code)) {
            /*添加时先验证是否重名*/
            Userinfo userinfo = new Userinfo();
            BeanUtils.copyProperties(registerForm, userinfo);
            Userinfo user = new Userinfo();
            user.setUsername(userinfo.getUsername());
            if (!userinfoMapper.getAll(user).isEmpty()) {
                throw new RTException("用户名已注册！");
            }
            user = new Userinfo();
            user.setPhone(userinfo.getPhone());
            if (!userinfoMapper.getAll(user).isEmpty()) {
                throw new RTException("手机号已注册！");
            }
            userinfo.setPassword(DigestUtils.md5Hex(userinfo.getPassword()));
            userinfo.setRole(2);
            userinfo.setNickname("用户" + userinfo.getPhone().substring(userinfo.getPhone().length() - 5));
            userinfoMapper.insertSelective(userinfo);
        }
    }

    @Override
    public void resetPassword(RegisterForm registerForm) {
        Code code = new Code();
        BeanUtils.copyProperties(registerForm, code);
        if (codeService.checkCode(code)) {
            Userinfo userinfo = new Userinfo();
            userinfo.setPhone(registerForm.getPhone());
            List<Userinfo> userinfoList = userinfoMapper.getAll(userinfo);
            if (userinfoList.isEmpty()) {
                throw new RTException("手机号不存在！");
            }
            if (userinfoList.size() > 1) {
                throw new RTException("手机号异常!");
            }
            userinfo = userinfoList.get(0);
            if (userinfo.getStatus() != 1) {
                throw new RTException("账号异常！");
            }
            Userinfo user = new Userinfo();
            user.setPassword(DigestUtils.md5Hex(registerForm.getPassword()));
            user.setUserinfoId(userinfo.getUserinfoId());
            userinfoMapper.updateByPrimaryKeySelective(user);
        }
    }

    public List<Userinfo> getHotAuthor() {
        return userinfoMapper.getHotAuthorList();
    }

    public List<Userinfo> getList(Userinfo userinfo) {
        return userinfoMapper.getAll(userinfo);
    }
}
